<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="商品名称" prop="goodsName" label-width="70px">
        <el-input v-model="queryParams.goodsName" placeholder="商品名称" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品编号" prop="goodsNo" label-width="70px">
        <el-input v-model="queryParams.goodsNo" placeholder="商品编号" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="场馆" prop="venueId" label-width="68px">
        <el-select v-model="queryParams.venueId" placeholder="场馆选择" clearable filterable style="width: 140px">
          <el-option v-for="dict in this.venueList" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="转场前日期">
        <el-date-picker v-model="queryParams.originDay" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="转场后日期">
        <el-date-picker v-model="queryParams.afterDay" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :height="tableHeight" v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange" @row-dblclick="handleUpdate" stripe border>
      <el-table-column v-if="this.queryParams.isPutaway" type="selection" width="55">
      </el-table-column>
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="商品名称" prop="goodsName" width="150" />
      <el-table-column label="商品编号" prop="goodsNo" width="150" align="center" />
      <el-table-column label="场馆名称" prop="venueName" width="120" align="center" />
      <el-table-column label="转前UID" prop="belonging" width="120" align="center" />
      <el-table-column label="转场前时间" prop="originDay" width="120" align="center" />
      <el-table-column label="转场后时间" prop="afterDay" width="120" align="center" />
      <el-table-column label="操作时间" prop="createTime" width="160" align="center" />
      <el-table-column label="操作人" prop="sysUserName" align="center" />
      <el-table-column label="操作人电话" prop="sysUserPhone" min-width="160" align="center" />

    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { listGoodsTurnAb } from "@/api/showroom/goods";
import { listVenue } from "@/api/showroom/venue";
import tableImage from "@/components/TableImageView/index.vue";
import { goodsPayTypeEnum } from '@/common/enums'
import { deepClone } from '@/utils/index'
import { checkPermi } from "@/utils/permission"; // 权限判断函数
import { compare } from "@/utils/index"

export default {
  name: "Lot", //竞价品信息
  components: { tableImage },
  dicts: [
    "goods_isResell_status",
    "goods_putaway_status",
  ],
  data() {
    return {
      goodsPayTypeEnum,
      //是否显示止售日期搜索框
      isShowDate: false,
      openAB: false,
      //上传的图片
      dialogImageUrl: "",
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      goodsList: [], //商品列表
      venueList: [], //场馆列表
      dateRange: [], //日期区间
      createDateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示添加弹窗
      add_open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
      },
      pageNum: 1,

      // 表单参数
      form: {},
      abForm: {},
      typeList: [
        { label: "竞价品", value: 1 },
        { label: "兑换商品", value: 4 },
      ],

      resellStatus: [
        { label: "未售出", value: 0 },
        { label: "已售出", value: 1 },
        { label: "已止售", value: 2 },
      ],
      isResell: [{ label: '未被抢购', value: 0 }, { label: '已被抢购', value: 1 }, { label: '已止售', value: 2 }],


      resellStatus: [
        { label: "止售区", value: "zp" },
        { label: "非止售区", value: "fzp" },
      ],

      abToday: [
        { label: "今天场", value: 0 },
        { label: "明天场", value: 1 },
      ],

      belonging: [{ label: "机器人回收", value: 1 }, { label: "非机器人回收", value: 0 }],

      batchUpParams: {
        ids: [],
        isPutaway: undefined
      },
      rules: {
        abToday: [{ required: true, message: "转场", trigger: "blur" }],
      },
      lotVenueList: []
    };
  },
  watch: {
    "queryParams.resellStatus": {
      deep: true,
      handler: function (newV, oldV) {
        this.isShowDate = newV == "zp" ? true : false;
      },
    },

    "queryParams.isPutaway": {
      deep: true,
      handler: function (newV, oldV) {
        this.batchUpParams.isPutaway = newV == 1 ? 0 : 1; //上下架取反
      },
    },

    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {

        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
    createDateRange(newV, oldV) {
      if (newV) {
        if (newV.length >= 2) {
          this.queryParams.createTimeStart = String(newV[0]);
          this.queryParams.createTimeEnd = String(newV[1]);
        }
      }
      else {
        this.queryParams.createTimeStart = undefined;
        this.queryParams.createTimeEnd = undefined;
      }
      this.getList();
    },
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    },
    isResellList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.isResell));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
    //状态列表添加全部
    goodsTypeList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.typeList));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
    //转售状态添加全部
    resellAllList() {
      let typeDictArray = JSON.parse(
        JSON.stringify(this.resellStatus)
      );
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
    //止售区域状态
    resellStatusList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.resellStatus));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
    //上架状态
    putawayStatusList() {
      let typeDictArray = JSON.parse(
        JSON.stringify(this.dict.type.goods_putaway_status)
      );
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
  },
  created() {
    this.getList();
    this.getVenueList();
  },
  methods: {
    checkPermi,
    getList() {
      this.loading = true;
      listGoodsTurnAb(this.queryParams)
        .then((response) => {
          if (response.code == 200) {
            this.goodsList = response.data;
            this.total = response.total;
          }
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    /**获取场馆列表 */
    getVenueList() {
      listVenue(this.queryParams)
        .then((response) => {
          if (response.code == 200) {

            let list = response.data;
            // list.sort(compare('id', true));

            this.venueList = list.filter((item, index, arr) => {
              return item.venueType == 0;
            })

          }
        })
        .catch((err) => { });
    },

    //列表选中
    handleSelectionChange(val) {
      this.seletVenueArray = val;
      this.batchUpParams.ids = val.map((item) => {
        return item.id;
      });
    },

    //下架
    batchUp(row) {
      this.batchUpParams.ids = [row.id];
      this.batchUpParams.isPutaway = 0;
      this.$modal
        .confirm('是否确认下架商品编号"' + row.goodsNo + '的商品"？')
        .then(() => {
          this.changeLotStatus();
        }
        );
    },

    /**一键上下架 */
    changeLotStatus() {
      if (!this.batchUpParams.ids) {
        this.$modal.msgError("请选择需要操作的商品");
        return;
      }
      batchUpGoodsInfo(this.batchUpParams).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      // this.resetForm("form");
      this.form = {};
      this.abForm = {};
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.endPrice = undefined;
      this.queryParams.createDateRange = undefined;
      this.queryParams.createTimeStart = undefined;
      this.createDateRange = undefined;
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加竞价品";
    },

    clearAbToday() {
      this.queryParams.abToday = undefined;
    },


    /**上传数据 */
    uploaded(params, dict) {
      this.form[params] = JSON.stringify(dict);
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "编辑竞价品";
      this.form = deepClone(row);
      this.form.isPutaway = String(this.form.isPutaway);
      this.form.payType = String(this.form.payType);
      this.form.artVenues = String(this.form.artVenues);
      this.open = true;
    },

    /** 提交按钮 */
    submitForm(refStr) {
      let ref = this.$refs[refStr];
      let valueData = ref.$refs.editor.currentValue;
      this.form.details = valueData;
      this.form.imgMin = this.form.minList;

      ref.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            editGoodsInfo(this.form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodsInfo(this.form).then((response) => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    openABChange(row) {
      this.abForm = row;
      this.openAB = true;
      this.title = "转场";
    },

    //转场
    postSaveGoodsTurnAb(row) {
      let params = {
        "goodsId": this.abForm.id,
        "venueId": this.abForm.venueId,
        "orderResellId": this.abForm.orderResellId,
        "abToday": this.abForm.abToday,
        "belonging": 1,
        "applyState": 1
      };
      this.$refs["abForm"].validate((valid) => {
        if (valid) {
          saveGoodsTurnAb(params).then((response) => {
            this.$modal.msgSuccess("转场");
            this.openAB = false;
            this.getList();
          });
        }
      });


    },

    /**上传数据 */
    uploaded(params, dict) {
      this.form[params] = JSON.stringify(dict);
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除商品编号为"' + row.goodsNo + '"的数据项？')
        .then(() => {
          delGoodsInfo({ goodsInfoId: row.id }).then((response) => {
            this.$modal.msgSuccess("删除成功");
            this.getList();
          });
        }
        ).catch(() => { });
    },

    /** 导出按钮操作 */
    exportExcel() {
      this.queryParams.beanName = 'GoodsInfoExport',
        this.queryParams.fileName = '竞价品',
        this.$alert('需要导出 "' + this.total + '" 条数据,是否确认导出', '提示', {
          confirmButtonText: '确定',

        }).then(() => {
          excelExportApi(this.queryParams, '/pms');
        });
    },
  },
};
</script>
<style scoped>
.el-dialog-div {
  height: 60vh;
  overflow: auto;
}
.input-W {
  width: 100%;
}
</style>
